/* ************************************************************************

   Copyright:
     2008 openWorks

   License:
     LGPL: http://www.gnu.org/licenses/lgpl.html
     See the LICENSE file in the project's top-level directory for details.

   Authors:
     * Martín Alejandro Paz

************************************************************************ */

/**
* Generador de menu que simplifica el uso de las clases {@link qx.ui.menubar.MenuBar} y {@link qx.ui.menubar.MenuButton}. 
* 
* *Ejemplo:*
*
* <pre class='javascript'>
* var accionesMenuPrincipal =
*  [
*   {name:"subMenu1", value:"subMenu1", icon:"iconoSubMenu1.png"},
*   {name:"accion1", value:"accion1"},
*   {name:"accion2", value:"accion2" , icon:"iconoAccion.png"}
*  ]
* var accionesSubMenu1 =
*  [
*   {name:"accion1", value:"accion1"},
*   {name:"accion2", value:"accion2", icon:"iconoAccion.png"},
*   {name:"accion3", value:"accion3"}
*  ]
* var menuPrincipal = new qx.ui.form.ow.Menu(accionesMenuPrincipal);
* menuPrincipal.setSubMenu("subMenu1", accionesSubMenu1);
*  </pre>
*/

qx.Class.define("qx.ui.form.ow.Menu",
{
	extend : qx.ui.container.Composite,
	/**
	* @param menu{String[]} Array con los elementos/acciones que contendra el menu.
	*/
	construct : function (menu)
	{
		this.base(arguments);

		this.setLayout(new qx.ui.layout.HBox(1));

		this._menuBar = new qx.ui.menubar.MenuBar;
		this._menuBar.setWidth(600);

		this._titles = [];

		for (r in menu)
		{
			//*************** EL 'IF' ES PARCHE PARA INTERNET EXPLORER **********************
			if (typeof menu[r] != 'function')
			{
	 			this._titles[menu[r].name] = new qx.ui.menubar.Button(menu[r].value, menu[r].icon);
	 			this._menuBar.add(this._titles[menu[r].name])
			}
		}

		this.add(this._menuBar);
	},
	
/*
*****************************************************************************
     MEMBERS
*****************************************************************************
*/
	members :
	{
		_menuBar : null,
		_titles : null,
		
		/**
		* Asigna un submenu a un elemento/acción del menu.
		* @param name{String} nombre del elemento del menu que activara el submenu.
		* @param subMenu{String[]} Array con los elementos/acciones que contendra el submenu.
		* @return {String[]}
		*/
		setSubMenu : function (name, subMenu, base)
		{
			
			var menu = new qx.ui.menu.Menu;

			for (r in subMenu)
			{
				//*************** EL 'IF' ES PARCHE PARA INTERNET EXPLORER **********************
				if (typeof subMenu[r] != 'function')
				{
					this._titles[subMenu[r].name] = new qx.ui.menu.Button(subMenu[r].value, subMenu[r].icon);
					menu.add(this._titles[subMenu[r].name])

					if (subMenu[r].execute != null)
					{
						if (base != null)
						{
							this._titles[subMenu[r].name].addListener("execute", subMenu[r].execute, base);
						}
						else
						{
							this._titles[subMenu[r].name].addListener("execute", subMenu[r].execute);
						}
					}
				}
			}

			this._titles[name].setMenu(menu);
		}
	}
});
